<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://unpkg.com/vue@2"></script>
    <script src="https://unpkg.com/vuex@3"></script>
  </head>

  <body>
    <!-- <div id="app">
      <button @click="$store.commit('increment')">++</button>
      <p>you clicked {{$store.state.count}} times - {{msg}}</p>
      <button @click="$store.commit('decrement')">--</button>

      <hr />
      <test-com />
    </div>

    <script>
      Vue.use(Vuex);

      let store = new Vuex.Store({
        state: {
          count: 10,
        },
        mutations: {
          increment(state) {
            state.count++;
          },
          decrement(state) {
            state.count--;
          },
        },
      });

      Vue.component("test-com", {
        template: "<h6>TestCom {{$store.state.count}}</h6>",
      });

      new Vue({
        el: "#app",
        store,
        data: function () {
          return {
            msg: "hello vuex",
          };
        },
      });
    </script> -->

    <!-- <div id="app">
      <button @click="$store.commit('increment')">++</button>
      <p>you clicked {{countFromStore}} times - {{msg}}</p>
      <button @click="$store.commit('decrement')">--</button>

      <hr />
      <test-com />
    </div>

    <script>
      Vue.use(Vuex);

      let store = new Vuex.Store({
        state: {
          count: 10,
          person: {
            name: 'nick',
            age: 17
          }
        },
        mutations: {
          increment(state) {
            state.count++;
          },
          decrement(state) {
            state.count--;
          },
        },
      });

      Vue.component("test-com", {
        template: "<h6>TestCom {{count}}</h6>",
        computed:{
            count: function () {
                return this.$store.state.count
            }
        }
      });

      new Vue({
        el: "#app",
        store,
        data: function () {
          return {
            msg: "hello vuex",
          };
        },
        computed: {
          countFromStore: function () {
            return this.$store.state.count;
          },
          person: function () {
            return this.$store.state.person
          }
        },
      });
    </script> -->

    <!-- mapState -->

    <div id="app">
      <button @click="$store.commit('increment')">++</button>
      <p>you clicked {{count}} times - {{msg}}</p>
      <button @click="$store.commit('decrement')">--</button>

      <hr />
      <test-com />
    </div>

    <script>
      Vue.use(Vuex);
      let { Store, mapState } = Vuex;

      let store = new Store({
        state: {
          count: 10,
          person: {
            name: "nick",
            age: 17,
          },
        },
        mutations: {
          increment(state) {
            state.count++;
          },
          decrement(state) {
            state.count--;
          },
        },
      });

      Vue.component("test-com", {
        template: "<h6>TestCom {{count}}</h6>",
        computed: {
          count: function () {
            return this.$store.state.count;
          },
        },
      });

      new Vue({
        el: "#app",
        store,
        data: function () {
          return {
            msg: "hello vuex",
          };
        },

        // 计算属性原始写法
        // computed: {
        //   countFromStore: function () {
        //     return this.$store.state.count;
        //   },
        //   person: function () {
        //     return this.$store.state.person
        //   }
        // },

        // mapState 辅助函数 参数为对象 （store里面状态的名字现在需要换）
        // 计算属性 的值 是一个对象
        // mapState 辅助函数 生成一个对象
        // computed: mapState({
        //   count: (state) => state.count,
        //   person007: (state) => state.person,
        // }),

        // mapState 辅助函数 参数为数组
        // computed: mapState(["count", "person"]),

        // 多个计算属性合并 后面会覆盖前面
        // computed: mapState(["count", "person"]),
        // computed: {
        //   x: function () {
        //     return Math.random();
        //   },
        // },

        // 计算属性的值 是一个对象 借助es6语法 合并多个对象属性
        computed: {
          ...mapState(["count", "person"]),
          ...mapState({
            person007: (state) => state.person,
          }),
          x: function () {
            return Math.random();
          },
        },
      });
    </script>
  </body>
</html>